<?php
/**
count by where clause
<!--[count_from_db table="innoforum_topics" group_by="tpc_type_id" where="tpc_type_id = '3'"]-->
*/
function smarty_function_return_count_from_db ($params, &$smarty) 
{
    $table        = $params['table'];
    $where        = $params['where'];
    $where_field  = $params['where_field'];
    $where_param  = $params['where_param'];
    $label_count  = $params['label_count'];
    $extend_field = $params['extend_field'];
    $order_by     = $params['order_by'];
    $group_by     = $params['group_by'];
    $fixed        = $params['fixed'];
    $limit        = $params['limit'];

    $table = DBUtil::getLimitedTablename($table);

    if ($table&&$fixed){
        $where_fixed = "where ".$fixed." = ".$where;
        $sql = "SELECT COUNT(*) FROM $table $where_fixed $group_by $order_by";
        $column = array("count");
        $result = DBUtil::executeSQL($sql);
        $objectArray = DBUtil::marshallObjects ($result, $column);
        $value = $objectArray['0']['count'];
        if(!empty($value)){
          $smarty->assign('return_count', $value);
        }
        return '';

    }else if($table && $label_count){

        $where = DBUtil::_checkWhereClause($where);
        $order_by =DBUtil::_checkOrderByClause($order_by);
        $group_by =DBUtilEx::_checkGroupByClause($group_by);

        if($extend_field){
          $limit = "LIMIT 0,".$limit;
          $sql = "SELECT COUNT(*) $label_count ,$extend_field FROM $table $where $group_by $order_by $limit ";
        }else{
          $sql = "SELECT COUNT(*) $label_count FROM $table $where $group_by $order_by $limit";
        }

        $column = array("count","label");
        $result = DBUtil::executeSQL($sql);
        $objectArray = DBUtil::marshallObjects ($result, $column);

        if(!empty($objectArray)){
          $smarty->assign("user_reccommend", $objectArray);
          //$smarty->assign("user_reccommend", $value);
        }

    }else if ($table && $where_field ){
        $where = DBUtil::_checkWhereClause($where);
        $order_by =DBUtil::_checkOrderByClause($order_by);
        $group_by =DBUtilEx::_checkGroupByClause($group_by);

        $sql = "SELECT COUNT(*) FROM $table WHERE $where_field = $where_param $group_by $order_by";
        $column = array("count");
        $result = DBUtil::executeSQL($sql);
        $objectArray = DBUtil::marshallObjects ($result, $column);
        $value = $objectArray['0']['count'];
        if(!empty($value)){
          $smarty->assign('return_count', $value);
        }
        return '';
    }else if ($table){
        $where = DBUtil::_checkWhereClause($where);
        $order_by =DBUtil::_checkOrderByClause($order_by);
        $group_by =DBUtilEx::_checkGroupByClause($group_by);

        $sql = "SELECT COUNT(*) FROM $table $where $group_by $order_by";
        $column = array("count");
        $result = DBUtil::executeSQL($sql);
        $objectArray = DBUtil::marshallObjects ($result, $column);
        $value = $objectArray['0']['count'];
        if(!empty($value)){
          $smarty->assign('return_count', $value);
        }
        return '';
    }



    //return true;
}
?>